/*global module:false*/
module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
    banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
      '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
      '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
      '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
      ' Licensed <%= pkg.license %> */\n',

    // Task configuration.
    concat: {
      options: {
        banner: '<%= banner %>',
        stripBanners: true
      },
      dist: {
        src: ['lib/<%= pkg.name %>.js'],
        dest: 'dist/<%= pkg.name %>.js'
      }
    },

    uglify: {
      options: {
        banner: '<%= banner %>'
      },
      dist: {
        src: '<%= concat.dist.dest %>',
        dest: 'dist/<%= pkg.name %>.min.js'
      }
    },

    jshint: {
      options: {
        curly: true,
        eqeqeq: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        unused: true,
        boss: true,
        eqnull: true,
        browser: true,
        globals: {}
      },
      gruntfile: {
        src: 'Gruntfile.js'
      },
      lib_test: {
        src: ['lib/**/*.js', 'test/**/*.js']
      }
    },


    qunit: {
      files: ['test/**/*.html']
    },


    watch: {
      gruntfile: {
        files: '<%= jshint.gruntfile.src %>',
        tasks: ['jshint:gruntfile']
      },
      lib_test: {
        files: '<%= jshint.lib_test.src %>',
        tasks: ['jshint:lib_test', 'qunit']
      }
    },

    // Build the documentation files
    includes: {
      target: {
        options: {
          includePath: 'html-src/include',
          banner: '<!-- 后台管理系统\r\n生成时间：<%= grunt.template.today() %> -->\r\n',
          silent     : true,
        },
        files: [{
          src    : ['*.html','*/*.html'], // Source files
          dest   : 'html-dist/', // Destination directory
          cwd    : 'html-src/build'
        }]
      }
    },

    // Replace image paths in AdminLTE without plugins
    replace: {
      files: {
        src: ['html-dist/*.html'],
        overwrite: true,                 // overwrite matched source files
        replacements: [{
          from: "@bower_components/",
          to: "../bower_components/"
        },
        {
          from: "@language",
          to  : "zh"
        }
        ]
      },
      files1: {
        src: ['html-dist/*/*.html'],
        overwrite: true,                 // overwrite matched source files
        replacements: [{
          from: "@bower_components/",
          to: "../../bower_components/"
        },
        {
          from: "@language",
          to  : "zh"
        }],
        cwd: 'html-dist'
      },
      files2: {
        src: ['html-dist/*/*/*.html'],
        overwrite: true,                 // overwrite matched source files
        replacements: [{
          from: "@bower_components/",
          to: "../../../bower_components/"
        },
        {
          from: "@language",
          to  : "zh"
        }]
      }
    },

    // 编译html文件
    includereplace: {
      dist: {
        options: {
          globals: {},
          includesDir: 'html-src/build/include',
          docroot:'.'
        },
        src    : ['*.html','*/*.html','!include/*.html'],
        dest   : 'html-dist/',
        cwd    : 'html-src/build'
      }
    },

    // Validate Bootstrap HTML
    bootlint: {
      options: {
        relaxerror: ['W005']
      },
      files  : ['pages/**/*.html', '*.html']
    }
  });

  // Concatenate JS files
  grunt.loadNpmTasks('grunt-contrib-concat');

  // Compress JS Files
  grunt.loadNpmTasks('grunt-contrib-uglify');

  //测试代码
  grunt.loadNpmTasks('grunt-contrib-qunit');

  // Validate JS code
  grunt.loadNpmTasks('grunt-contrib-jshint');

  // Watch File Changes
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Lint Bootstrap
  grunt.loadNpmTasks('grunt-bootlint');  

  // Delete not needed files
  grunt.loadNpmTasks('grunt-contrib-clean');

  // Include Files Within HTML
  grunt.loadNpmTasks('grunt-includes');

  // Include Files Within HTML
  grunt.loadNpmTasks('grunt-include-replace');


  // Replace
  grunt.loadNpmTasks('grunt-text-replace');

  // Linting task
  //grunt.registerTask('lint', ['jshint', 'bootlint']);

  // JS task
  //grunt.registerTask('js', ['concat', 'uglify']);

  // Default task.
  //grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

  //grunt.registerTask('default', ['jshint', 'concat', 'uglify']);
  //grunt.registerTask('default', ['watch']);

  //grunt.registerTask('default', ['includereplace']);

  grunt.registerTask('default', ['includes','replace']);

};
